package com.jy.fxf_phone.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jy.fxf_phone.R;

/**
 * Created by qiantao on 2016/11/16.
 * 食物背包
 * 使用setOnFoodsClickListener设置食物的点击监听事件
 */

public class FoodLayout extends RelativeLayout implements View.OnClickListener {
    private Context mContext;
    private boolean isClosed = true;//背包是否关闭
    public interface FoodsClickListener{
        void onFoodsClick(int foodNumber);
    }
    private FoodsClickListener mFoodsClickListener;

    public void setOnFoodsClickListener(FoodsClickListener listener) {
        mFoodsClickListener  = listener;
    }

    ImageView mBagIv;
    RelativeLayout mFoodsRl;
    CircleLayout mFoodsCl;
    View mCircleV;
    ImageView food1Iv;
    ImageView food2Iv;
    ImageView food3Iv;
//    ImageView food4Iv;
//    ImageView food5Iv;
//    ImageView food6Iv;

    public FoodLayout(Context context) {
        this(context, null);
    }

    public FoodLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.layout_food, this);
        mBagIv = (ImageView) findViewById(R.id.iv_bag);
        mFoodsRl = (RelativeLayout) findViewById(R.id.rl_foods);
        mFoodsCl = (CircleLayout) findViewById(R.id.cl_food);
        mCircleV = findViewById(R.id.view_circle);
        mBagIv.setOnClickListener(this);

        food1Iv = (ImageView) findViewById(R.id.iv_food_1);
        food2Iv = (ImageView) findViewById(R.id.iv_food_2);
        food3Iv = (ImageView) findViewById(R.id.iv_food_3);
//        food4Iv = (ImageView) findViewById(R.id.iv_food_4);
//        food5Iv = (ImageView) findViewById(R.id.iv_food_5);
//        food6Iv = (ImageView) findViewById(R.id.iv_food_6);

        food1Iv.setOnClickListener(this);
        food2Iv.setOnClickListener(this);
        food3Iv.setOnClickListener(this);
//        food4Iv.setOnClickListener(this);
//        food5Iv.setOnClickListener(this);
//        food6Iv.setOnClickListener(this);

    }

    //展示食物动画
    private AnimatorSet showAnim() {
        mFoodsCl.setAlpha(1f);
        mFoodsCl.setScaleX(1f);
        mFoodsCl.setScaleY(1f);
        ObjectAnimator moveToLeftAnim = ObjectAnimator.ofFloat(mFoodsRl, "translationX",
                0f, -180f);
        ObjectAnimator enlargeAnimX = ObjectAnimator.ofFloat(mCircleV, "scaleX", 1f, 7f);
        ObjectAnimator enlargeAnimY = ObjectAnimator.ofFloat(mCircleV, "scaleY", 1f, 7f);
        moveToLeftAnim.setInterpolator(new AccelerateInterpolator(2f));
        enlargeAnimX.setInterpolator(new BounceInterpolator());
        enlargeAnimY.setInterpolator(new BounceInterpolator());
        AnimatorSet set = new AnimatorSet();
        set.play(enlargeAnimX).with(enlargeAnimY).after(moveToLeftAnim);
        set.setDuration(1000);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                mBagIv.setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mFoodsCl.setVisibility(VISIBLE);
                mBagIv.setEnabled(true);
            }
        });
        return set;
    }

    //隐藏食物动画
    private AnimatorSet hideAnim() {
        ObjectAnimator moveToRightAnim = ObjectAnimator.ofFloat(mFoodsRl, "translationX",
                -180f,0f);
        ObjectAnimator narrowAnimX = ObjectAnimator.ofFloat(mCircleV, "scaleX", 7f, 1f);
        ObjectAnimator narrowAnimY = ObjectAnimator.ofFloat(mCircleV, "scaleY", 7f, 1f);
        moveToRightAnim.setInterpolator(new DecelerateInterpolator(3f));
//        narrowAnimX.setInterpolator(new AccelerateInterpolator());
//        narrowAnimY.setInterpolator(new AccelerateInterpolator());
        ObjectAnimator foodsAlpha = ObjectAnimator.ofFloat(mFoodsCl, "alpha", 1f, 0f);
        ObjectAnimator foodsScaleX = ObjectAnimator.ofFloat(mFoodsCl, "scaleX", 1f, 0f);
        ObjectAnimator foodsScaleY = ObjectAnimator.ofFloat(mFoodsCl, "scaleY", 1f, 0f);
        AnimatorSet circleSet = new AnimatorSet();
        circleSet.play(narrowAnimX).with(narrowAnimY).with(foodsAlpha).with(foodsScaleX).with(foodsScaleY).before(moveToRightAnim);
        circleSet.setDuration(1000);

        circleSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                mBagIv.setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mFoodsCl.setVisibility(INVISIBLE);
                mBagIv.setEnabled(true);
            }
        });
        return circleSet;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_bag:
                showBag();
                break;
            case R.id.iv_food_1:
                if (mFoodsClickListener != null) {
                    mFoodsClickListener.onFoodsClick(1);
                }
                break;
            case R.id.iv_food_2:
                if (mFoodsClickListener != null) {
                    mFoodsClickListener.onFoodsClick(2);
                }
                break;
            case R.id.iv_food_3:
                if (mFoodsClickListener != null) {
                    mFoodsClickListener.onFoodsClick(3);
                }
                break;
//            case R.id.iv_food_4:
//                if (mFoodsClickListener != null) {
//                    mFoodsClickListener.onFoodsClick(4);
//                }
//                break;
//            case R.id.iv_food_5:
//                if (mFoodsClickListener != null) {
//                    mFoodsClickListener.onFoodsClick(5);
//                }
//                break;
//            case R.id.iv_food_6:
//                if (mFoodsClickListener != null) {
//                    mFoodsClickListener.onFoodsClick(6);
//                }
//                break;
        }
    }

    private void showBag() {
        if (isClosed) {
            openBag();
            isClosed = !isClosed;
        } else {
            closeBag();
            isClosed = !isClosed;
        }
    }

    /**
     * 打开背包
     */
    private void openBag() {
        showAnim().start();
    }

    /**
     * 关闭背包
     */
    private void closeBag() {
        hideAnim().start();
    }
}
